﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace Cs期中作业
{
    public partial class 登陆 : Form
    {
        public 登陆()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (RemUser.Checked == true)
                Gen.WritePrivateProfileString("Client", "UserName", UserT.Text, @".\config.ini");
            else
                Gen.WritePrivateProfileString("Client", "UserName", "", @".\config.ini");
            //定义数据库连接语句:服务器= 数据库名= 用户名= 密码=
            string consqlserver = "Data Source=" + ip + ";Initial Catalog=" + db + ";Persist Security Info=True;UID=" + sqlUser + ";Password=" + sqlPw;
            //定义SQL Server连接对象
            SqlConnection con = new SqlConnection(consqlserver);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select password from " + liSh + " where username='" + UserT.Text + "'";
            cmd.Connection = con;
            string pw1Sql = Convert.ToString(cmd.ExecuteScalar());
            if (PwMsk.Text == pw1Sql)
            {

                MessageBox.Show("欢迎进入高校宿舍管理系统", "登陆成功");
                this.Hide();
                new 主菜单().Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误请重新确输入", "登录失败");
            }
            con.Close();
        }

        private void About_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("Copyright © 2016-2019 MingZhi. All Rights Reserved.\r\n明挚云 版权所有", "关于我们");
        }

        private void Login_Load(object sender, EventArgs e)
        {
            StringBuilder ConVerS = new StringBuilder();
            StringBuilder UserS = new StringBuilder();
            StringBuilder ipS = new StringBuilder();
            StringBuilder dbS = new StringBuilder();
            StringBuilder liShS = new StringBuilder();
            StringBuilder sqlUserS = new StringBuilder();
            StringBuilder sqlPwS = new StringBuilder();
            double NowConfVer = 1.0, ConVer;

            //如果配置文件不存在(第一次使用)
            if (File.Exists(@".\config.ini") == false)
            {
                Gen.WritePrivateProfileString("Software", "Version", "1.0", @".\config.ini");
                Gen.WritePrivateProfileString("Host", "Server", "dorm.mcland.top", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "Database", "Dorm", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "LoginSheet", "SysAdmin", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "StudentSheet", "Student", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "DormSheet", "Dorm", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "CheckInSheet", "CheckIn", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "UserName", "Dorm", @".\config.ini");
                Gen.WritePrivateProfileString("SQL", "Password", "12345679", @".\config.ini");
                Gen.WritePrivateProfileString("Client", "UserName", "", @".\config.ini");
            }
            //获取当前配置文件版本号
            Gen.GetPrivateProfileString("Software", "Version", "0.0", ConVerS, 255, @".\config.ini");
            ConVer = Convert.ToDouble(ConVerS.ToString());
            //检测配置文件版本号
            if (ConVer != NowConfVer)
            {
                //若配置文件版本旧
                if (ConVer < NowConfVer)
                {
                    //将旧文件重命名为config.ini.old
                    MessageBox.Show("配置文件版本过低，将生成全新默认的配置文件，旧文件将被重命名为config_old.ini", "配置文件版本过旧");
                    File.Move(@".\config.ini", @".\config.ini.old");
                }
                //若配置文件版本新
                else
                {
                    if (MessageBox.Show("配置文件是由新版本的程序生成的，请使用新版本的软件。是否继续？", "程序版本过旧", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)//如果点击“否”按钮
                    {
                        Application.Exit();
                    }
                }
            }
            //读取配置文件
            Gen.GetPrivateProfileString("Host", "Server", "localhost", ipS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "Database", "Dorm",dbS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "LiSheet", "SysAdmin", liShS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "UserName", "", sqlUserS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("SQL", "Password", "", sqlPwS, 255, @".\config.ini");
            Gen.GetPrivateProfileString("Client", "UserName", "", UserS, 255, @".\config.ini");
            //StringBuilder 转 String
            ip = ipS.ToString();
            db = dbS.ToString();
            liSh = liShS.ToString();
            sqlUser = sqlUserS.ToString();
            sqlPw = sqlPwS.ToString();
            if (UserS != null)
            {
                UserT.Text = UserS.ToString();
                RemUser.Checked = true;
            }
        }

        //StringBuilder 转 String
        string ip, db, liSh, sqlUser, sqlPw;




    }
}